<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>reverse_nested 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-bucket-reverse-nested-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-reverse-nested-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-reverse-nested-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-bucket-reverse-nested-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-bucket-reverse-nested-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-bucket.html">桶(bucket)聚合</a></span>
»
<span class="breadcrumb-node">reverse_nested 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-bucket-rare-terms-aggregation.html">« rare_terms 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-sampler-aggregation.html">sampler 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-bucket-reverse-nested-aggregation"></a>反向嵌套(reverse_nested)聚合
</h2>
</div></div></div>
<p>
一个特殊的单桶聚合，支持从嵌套文档聚合父文档。

这种聚合可以有效地打破嵌套块结构，并链接到其他嵌套结构或根文档，这允许在嵌套聚合中嵌套不属于嵌套对象的其他聚合。 
</p>
<p>
<code class="literal">reverse_nested</code>聚合必须定义在<code class="literal">nested</code>聚合内部。
</p>
<div class="ulist itemizedlist">
<p class="title"><strong>选项：</strong></p>
<ul class="itemizedlist">
<li class="listitem">
<code class="literal">path</code> - 定义应该将哪个嵌套对象字段联接回去。

默认值为空，这意味着它联接回根/主文档级别。

该路径(path)不能包含对嵌套对象字段的引用，该嵌套对象字段不在<code class="literal">reverse_nested</code>所在的<code class="literal">nested</code>聚合的嵌套结构中。
</li>
</ul>
</div>
<p>
例如，假设我们有一个包含问题(issue)和评论(comment)的票据系统的索引。

评论作为嵌套文档内联到问题文档中。

映射看起来可能像这样：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /issues
{
    "mappings": {
         "properties" : {
             "tags" : { "type" : "keyword" },
             "comments" : { <a id="CO238-1"></a><i class="conum" data-value="1"></i>
                 "type" : "nested",
                 "properties" : {
                     "username" : { "type" : "keyword" },
                     "comment" : { "type" : "text" }
                 }
             }
         }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/507.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO238-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
<code class="literal">comments</code>是一个数组，用于保存<code class="literal">issue</code>对象下的嵌套文档。
</p>
</td>
</tr>
</table>
</div>
<p>
下面的聚合将返回已发表评论的前几名评论者的用户名，以及这几个评论者各自对用户已发表评论的问题的前几个标签：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET /issues/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "comments": {
      "nested": {
        "path": "comments"
      },
      "aggs": {
        "top_usernames": {
          "terms": {
            "field": "comments.username"
          },
          "aggs": {
            "comment_to_issue": {
              "reverse_nested": {}, <a id="CO239-1"></a><i class="conum" data-value="1"></i>
              "aggs": {
                "top_tags_per_comment": {
                  "terms": {
                    "field": "tags"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/508.console"></div>
<p>
正如你在上面看到的，<code class="literal">reverse_nested</code>聚合被放入<code class="literal">nested</code>聚合中，因为这是 DSL 中唯一可以使用<code class="literal">reverse_nested</code>聚合的地方。

它的唯一目的是联接回嵌套结构中更高一级的父文档。
</p>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO239-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>
<code class="literal">reverse_nested</code> 聚合联接回根/主文档级别，因为尚未定义<code class="literal">path</code>。

如果在映射中定义了多层嵌套对象类型，通过<code class="literal">path</code>选项，<code class="literal">reverse_nested</code>聚合可以联接回不同的级别
</p>
</td>
</tr>
</table>
</div>
<p>可能的响应片段：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "aggregations": {
    "comments": {
      "doc_count": 1,
      "top_usernames": {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets": [
          {
            "key": "username_1",
            "doc_count": 1,
            "comment_to_issue": {
              "doc_count": 1,
              "top_tags_per_comment": {
                "doc_count_error_upper_bound" : 0,
                "sum_other_doc_count" : 0,
                "buckets": [
                  {
                    "key": "tag_1",
                    "doc_count": 1
                  }
                  ...
                ]
              }
            }
          }
          ...
        ]
      }
    }
  }
}</pre>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-bucket-rare-terms-aggregation.html">« rare_terms 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-sampler-aggregation.html">sampler 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>